<template>
	<view>
		<view @touchmove.prevent.stop style="z-index: 999;">
			<uni-popup ref="auth" :custom="true" :mask-click="false" @change="maskClick">
				<view class="uni-tip">
					<image class="tipIv" src="../../static/public/logo.jpg" mode=""></image>
					<view class="tipTv">女创未来</view>
					<view class="loginTv1" @click="onLoginClick()" v-if="!ismobile">快捷登录</view>
					<button v-if="ismobile" class="loginTv1" open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">绑定手机号</button>
					<view v-if="isShow" class="loginTv2" @click="onNoLoginClick()">暂不登录</view>
					<view class="xuzhiV">
						<image @click="xieyi=!xieyi" class="xuzhiIv"
							:src="xieyi?'../../static/select_.svg':'../../static/select.svg'"></image>
						<view class="xuzhiTv1">我已阅读并同意</view>
						<view class="xuzhiTv2" @click="onAgreementClick">《隐私协议》</view>
					</view>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'ns-login',
		data() {
			return {
				isShow: true,
				xieyi: false,
				ismobile:false
			};
		},
		created() {

		},
		mounted() {

		},
		methods: {
			open(data) {
				if (data) {
					this.isShow = data.show;
				}
				this.$refs.auth.open();
			},
			close() {
				this.$refs.auth.close();
			},
			maskClick(e) {
				let isShow = e.show;
				if (!isShow) {
					this.$emit("saveUserInfo", {
						code: 202
					})
				}
			},
			onNoLoginClick() {
				this.$refs.auth.close();
			},
			onLoginClick() {
				if (!this.xieyi) {
					uni.$u.toast("请先阅读并同意隐私协议")
					return
				}
				
				// #ifndef H5
				
				uni.login({
					provider: 'weixin',
					timeout: 3000,
					success: res => {
						if (res.code) {
							uni.getUserInfo({
								success: sres => {
									this.login(res.code, sres.encryptedData, sres.iv)
								},
								fail: () => {
									console.log(res)
					
									uni.$u.toast("请求失败")
								}
							})
						}
					},
					fail: () => {
						uni.$u.toast("请求失败")
					}
				})
				// #endif
				// #ifdef H5
				this.getmologin()
				return false;
				// #endif

			},
			login(code, encryptedData, iv) {
				this.$api.login({
					code: code,
					iv: iv,
					encryptedData: encryptedData,
				}).then((res) => {
					uni.$u.toast("登录成功")
					console.log(res)
					if(res.userinfo.mobile == '' ||!res.userinfo.mobile){
						this.ismobile = true
						uni.setStorageSync("user_info_token", res.userinfo.token);
						uni.setStorageSync("user_info_id", res.userinfo.id);
						uni.setStorageSync("user_info", res.userinfo);
					}else{
						this.ismobile = false
						uni.setStorageSync("user_info_token", res.userinfo.token);
						uni.setStorageSync("user_info_id", res.userinfo.id);
						uni.setStorageSync("user_info", res.userinfo);
						this.$emit("saveUserInfo", {
							code: 200
						})
						this.$refs.auth.close();
					}
					// return false;
					
				}).catch((e) => {
					uni.$u.toast(e.msg === undefined ? "登录失败" : e.msg)
				})
			},
			decryptPhoneNumber(e){ 
			console.log(e);
			// console.log("++++")
			// console.log(uni.getStorageSync('socketSessionId'))
			if (!this.xieyi) {
				uni.$u.toast("请先阅读并同意隐私协议")
				return
			}
			if(e.detail.errMsg == 'getPhoneNumber:ok'){
				// let result = sheep.$platform.useProvider().bindUserPhoneNumber(e.detail);
				// if (result) {
				// sheep.$store('user').getInfo();
				// closeAuthModal();
				// }
				
				this.$api.bindMiniMobile(e.detail).then((res) => {
					uni.$u.toast("绑定成功")
					console.log(res)
					this.ismobile = false
					this.$emit("saveUserInfo", {
						code: 200
					})
					this.$refs.auth.close();
					// if(res.userinfo.mobile == '' ||!res.userinfo.mobile){
					// 	this.ismobile = true
					// }else{
					// 	this.ismobile = false
					// 	uni.setStorageSync("user_info_token", res.userinfo.token);
					// 	uni.setStorageSync("user_info_id", res.userinfo.id);
					// 	uni.setStorageSync("user_info", res.userinfo);
					// 	this.$emit("saveUserInfo", {
					// 		code: 200
					// 	})
					// 	this.$refs.auth.close();
					// }
					// return false;
					
				}).catch((e) => {
					uni.$u.toast(e.msg === undefined ? "登录失败" : e.msg)
				})
				
				// const userInfo = await sheep.$store('user').setToken(res.data.token);
				// const userInfo = await sheep.$store('user').getInfo();
			}else{
				uni.$u.toast("请先授权获取手机号")
				return
			}
			
			},
			onAgreementClick() {
				this.$u.route({
					url: '/mine/me/Agreement',
					params: {
						type: 3,
					}
				})
			},
			getmologin(){
				//模拟登录 浏览器
				// 写死token
				let userinfo ={
					avatar:"https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
					bonus: "0.00",
					createtime: 1721801164,
					expires_in: 2592000,
					expiretime: 1724393164,
					id: 1,
					mobile: "",
					nickname: "微信用户",
					score: 0,
					shop_id: 1,
					token: "71fdcd64-3f9b-4c8f-a00a-34d0f68246e0",
					train_days: 0,
					train_nums: 0,
					user_id: 1,
					username: "微信用户",
				}
				uni.setStorageSync("user_info_token", '71fdcd64-3f9b-4c8f-a00a-34d0f68246e0');
				uni.setStorageSync("user_info_id", '1');
				uni.setStorageSync("user_info", userinfo);
				// 写死token结束
				this.$emit("saveUserInfo", {
					code: 200
				})
				this.$refs.auth.close();
			}

		}
	};
</script>

<style lang="scss">
	.uni-tip {
		width: 580rpx;
		background: #1f1f1f;
		box-sizing: border-box;
		border-radius: 10rpx;
		overflow: hidden;
		height: initial;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 30rpx;
		z-index: 9999;
	}

	.tipIv {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
	}

	.tipTv {
		font-size: 30rpx;
		color: #fff;
		margin-top: 30rpx;
	}

	.loginTv1 {
		width: 80%;
		margin-top: 70rpx;
		height: 70rpx;
		border-radius: 35rpx;
		background-color: #E6A5B4;
		color: #FFFFFF;
		font-size: 26rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.loginTv2 {
		width: 80%;
		margin-top: 30rpx;
		height: 70rpx;
		border-radius: 35rpx;
		background: rgba(158, 158, 158, 1);
		border: 1rpx solid #9e9e9e;
		color: #FFFFFF;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.xuzhiV {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 35rpx;

		.xuzhiIv {
			width: 30rpx;
			height: 30rpx;
		}

		.xuzhiTv1 {
			margin-left: 30rpx;
			font-size: 28rpx;
			color: #8D8D8D;
		}

		.xuzhiTv2 {
			font-size: 28rpx;
			color: #8D8D8D;
		}
	}
</style>
<style scoped>

</style>
